home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Online / Socks5 / src / server / packet.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-10  |  3.2 KB  |  104 lines

  1. /* Copyright (c) 1995-1999 NEC USA, Inc.  All rights reserved.               */
  2. /*                                                                           */
  3. /* The redistribution, use and modification in source or binary forms of     */
  4. /* this software is subject to the conditions set forth in the copyright     */
  5. /* document ("Copyright") included with this distribution.                   */
  6.  
  7. /*
  8.  * $Id: packet.c,v 1.11.4.3 1999/03/05 21:52:48 steve Exp $
  9.  */
  10.  
  11. #include "socks5p.h"
  12. #include "threads.h"
  13. #include "daemon.h"
  14. #include "packet.h"
  15. #include "log.h"
  16.  
  17. static const char *Addr2Ascii(const S5NetAddr *na) {
  18.     switch (na->sa.sa_family) {
  19.     case AF_S5NAME:
  20.         return na->sn.sn_name;
  21.     case AF_INET:
  22.         return inet_ntoa(na->sin.sin_addr);
  23.     default:
  24.         return "";
  25.     }
  26. }
  27.  
  28. static u_short Addr2Port(const S5NetAddr *na) {
  29.     switch (na->sa.sa_family) {
  30.     case AF_S5NAME:
  31.         return na->sn.sn_port;
  32.     case AF_INET:
  33.         return na->sin.sin_port;
  34.     default:
  35.         return (u_short)0;
  36.     }
  37. }
  38.  
  39. #define AddrAndPort(x)     Addr2Ascii((x)), ntohs(Addr2Port((x)))
  40.  
  41. static int PacketPrintFilter(S5Packet *inPacket, S5Packet *outPacket, S5LinkInfo *pri, void *option, int *dir, int *action) {
  42.     register u_int i,j;
  43.     register char *sp;
  44.     char buf[1024];
  45.  
  46.     /* Since we are only worried about what comes in, if this wasn't the     */
  47.     /* result of a read, return -1, indicating we did nothing...             */
  48.     if (*action != S5_ACTION_READ || !option) {
  49.     return -1;
  50.     }
  51.  
  52.     /* Ok the output of this packet, but print it first...                   */
  53.     *outPacket = *inPacket;
  54.     *action    = S5_ACTION_WRITE;
  55.  
  56.     /* Print out a header showing who sent what...                           */
  57.     S5LogUpdate(S5LogDefaultHandle, S5_LOG_INFO, 0, "PacketPrint: Sender: %s:%d", AddrAndPort((*dir == S5_DIRECTION_OUT)?&pri->srcAddr:&pri->dstAddr));
  58.     S5LogUpdate(S5LogDefaultHandle, S5_LOG_INFO, 0, "PacketPrint: Recver: %s:%d", AddrAndPort((*dir == S5_DIRECTION_OUT)?&pri->dstAddr:&pri->srcAddr));
  59.     S5LogUpdate(S5LogDefaultHandle, S5_LOG_INFO, 0, "PacketPrint: Packet contents (%d chars):", inPacket->off);
  60.     sprintf(buf, "PacketPrint:     ");
  61.     j = strlen("PacketPrint:     ");
  62.  
  63.     /* Print out the data contained in the packet...                         */
  64.     for (i = 0, sp = inPacket->data; i < inPacket->off; i++, sp++) {
  65.     if (isprint((unsigned char)*sp) && *sp != '\n') {
  66.         sprintf(buf+j, "%c", *sp);
  67.         j++;
  68.     } else if (j + 3 > 80) {
  69.         i--; sp--;
  70.         j += 3;
  71.     } else {
  72.         sprintf(buf+j, "\\%02x", (u_char)*sp);
  73.         j += 3;
  74.     }
  75.  
  76.     /* Try to break around 80 characters or when there was a newline in  */
  77.     /* the string itself...                                              */
  78.     if (j > 80 || *sp == '\n') {
  79.         S5LogUpdate(S5LogDefaultHandle, S5_LOG_INFO, 0, "%s", buf);
  80.         sprintf(buf, "PacketPrint:     ");
  81.         j = strlen("PacketPrint:     ");
  82.     }
  83.     }
  84.  
  85.     return 0;
  86. }
  87.  
  88. int PacketPrintSetup(S5LinkInfo *li, S5FilterInfo *finfo) {
  89.     MUTEX_LOCK(env_mutex);
  90.     finfo->option = getenv("SOCKS5_PRINTPACKET")?(void *)-1:0;
  91.     MUTEX_UNLOCK(env_mutex);
  92.  
  93.     if (!finfo->option) {
  94.     finfo->filter = NULL;
  95.     finfo->clean  = NULL;
  96.     return -1;
  97.     } else {
  98.     finfo->filter = PacketPrintFilter;
  99.     finfo->clean  = NULL;
  100.     return 0;
  101.     }
  102. }
  103.  
  104.